package com.icss.ah.shippingTemplate.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.alibaba.fastjson.JSONArray;
import com.icss.ah.base.action.BaseAction;
import com.icss.ah.login.bean.UserInfoBean;
import com.icss.ah.shippingTemplate.service.ShippingTemplateService;
import com.icss.j2ee.util.UUID;

/**
 * 
 * @author xuyl
 * 运费模板
 * 1、模板列表查询功能
 * 2、模板状态修改
 * 3、模板新增/编辑/详情/保存功能
 * 4、包邮条件查询
 * 5、省市县查询
 *
 */
@Controller
@Scope("prototype")
public class ShippingTemplateAction extends BaseAction {
	
	private static final long serialVersionUID = 124341434L;
	
	@Autowired
	private ShippingTemplateService shippingTemplateService;
	
	private String uuid;		//模板ID
	private String tempName;	//模板名称
	private String country;		//国家
	private String province;	//省
	private String city;		//市
	private String cityArea;	//区
	private String deliveryTime;//发货时间
	private String isFree;		//是否包邮0否，1是
	private String priceType;	//计价方式1按件数，2按金额
	private String defaultLimit;//默认起始件数/金额
	private String defaultCost;	//默认运费
	private String stepLimit;	//递增件数/金额
	private String stepCost;	//递增运费
	private String setFree;		//是否指定包邮条件
	private String status;		//状态1启用，0停用
	private String numStr;		//包邮条件数字符串
	private String pageFlag;	//页面标识

	/**
	 * 列表页
	 * @return
	 */
	public String toShippingTemplateListPage(){
		return "toShippingTemplateListPage";
	}
	
	/**
	 * 列表页查询
	 */
	public void queryShippingTemplateList(){
		Map<String, String> map = new HashMap<String, String>();
		UserInfoBean user = getUserInfo("");
		map.put("corpId", user.getCorpId());
		map.put("shopId", user.getShopId());
		map.put("tempName", tempName);
		map.put("isFree", isFree);
		map.put("priceType", priceType);
		map.put("status", status);
		map.put("startIndex", getStartIndexMysql());
		map.put("rowNums", getEndIndexMysql());
		dataGridJson = shippingTemplateService.queryShippingTemplateList(map);
		str2Resp(JSONObject.fromObject(dataGridJson).toString());
	}
	
	/**
	 * 状态修改
	 */
	public void updateStatus(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("uuid", uuid);
		map.put("status", status);
		String result = shippingTemplateService.updateStatus(map);
		str2Resp(result);
	}
	
	/**
	 * 删除运费模板
	 */
	public void delInfo(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("uuid", uuid);
		String result = shippingTemplateService.delInfo(map);
		str2Resp(result);
	}
	
	
	/**
	 * 新增/编辑/详情页
	 * @return
	 */
	public String toAddOrUpdatePage(){
		String page = "toAddOrUpdatePage";
		request = getRequest();
		Map<String, String> map = new HashMap<String, String>();
		map.put("level", "1");
		map.put("areaPcode", "");
		List<Map<String, Object>> countryList = shippingTemplateService.queryProvinceCityList(map);
		request.setAttribute("countryList", countryList);
		List<Map<String, Object>> timeList = shippingTemplateService.queryDeliveryTime();
		request.setAttribute("timeList", timeList);
		if(uuid != null && !"".equals(uuid)){
			Map<String, Object> bean = shippingTemplateService.queryInfo(uuid);
			String country = (String) bean.get("COUNTRY");
			map.put("level", "2");
			map.put("areaPcode", country);
			List<Map<String, Object>> provinceList = shippingTemplateService.queryProvinceCityList(map);
			String province = (String) bean.get("PROVINCE");
			map.put("level", "3");
			map.put("areaPcode", province);
			List<Map<String, Object>> cityList = shippingTemplateService.queryProvinceCityList(map);
			String city = (String) bean.get("CITY");
			map.put("level", "4");
			map.put("areaPcode", city);
			List<Map<String, Object>> cityAreaList = shippingTemplateService.queryProvinceCityList(map);
			request.setAttribute("bean", bean);
			request.setAttribute("provinceList", provinceList);
			request.setAttribute("cityList", cityList);
			request.setAttribute("cityAreaList", cityAreaList);
		}
		if(pageFlag != null && "info".equals(pageFlag)) page = "toInfoPage";
		return page;
	}
	
	/**
	 * 信息保存
	 */
	public void save(){
		UserInfoBean user = getUserInfo("");
		String corpId = user.getCorpId();
		String shopId = user.getShopId();
		String  flag = "update";
		if(uuid == null || "".equals(uuid)){
			uuid = new UUID().toString();
			flag = "insert";
		}
		Map<String, String> map = new HashMap<String, String>();
		map.put("uuid", uuid);
		map.put("corpId", corpId);
		map.put("shopId", shopId);
		map.put("flag", flag);
		map.put("tempName", tempName);
		map.put("country", country);
		if(null != country && !"001".equals(country)){
			province="";
			city="";
			cityArea="";
		}
		map.put("province", province);
		map.put("city", city);
		map.put("cityArea", cityArea);
		map.put("deliveryTime", deliveryTime);
		map.put("isFree", isFree);
		if(null!=isFree&&"1".equals(isFree)){//虚数消除数据影响
			priceType="0";
			defaultLimit="0";
			defaultCost="0";
			stepLimit="0";
			stepCost="0";
			setFree="0";
		}
		map.put("priceType", priceType);
		map.put("defaultLimit", defaultLimit);
		map.put("defaultCost", defaultCost);
		map.put("stepLimit", stepLimit);
		map.put("stepCost", stepCost);
		map.put("setFree", setFree);
		map.put("status", status);
		request = getRequest();
		List<Map<String, String>> list = new ArrayList<Map<String,String>>();
		if(numStr != null && numStr.length() > 0){
			String[] arr = numStr.split(",");
			for(int i=1;i<arr.length;i++){
				String str = arr[i];
				Map<String, String> o = new HashMap<String, String>();
				o.put("uuid", new UUID().toString());
				o.put("corpId", corpId);
				o.put("shopId", shopId);
				o.put("templateId", uuid);
				o.put("priceType", request.getParameter("priceType"+str));
				o.put("typeLimit", request.getParameter("typeLimit"+str));
				o.put("status", request.getParameter("status"+str));
				list.add(o);
			}
		}
		String result = shippingTemplateService.saveInfo(map,list);
		str2Resp(result);
	}
	
	/**
	 * 包邮条件
	 */
	public void queryItemList(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("uuid", uuid);
		map.put("startIndex", getStartIndexMysql());
		map.put("rowNums", getEndIndexMysql());
		dataGridJson = shippingTemplateService.queryItemList(map);
		str2Resp(JSONObject.fromObject(dataGridJson).toString());
	}
	
	private String level;
	private String areaPcode;
	
	/**
	 * 省市县区查询
	 */
	public void queryProvinceCityList(){
		Map<String, String> map = new HashMap<String, String>();
		map.put("level", level);
		map.put("areaPcode", areaPcode);
		List<Map<String, Object>> list = shippingTemplateService.queryProvinceCityList(map);
		str2Resp(JSONArray.toJSONString(list));
	}
	
	public String getUuid() {
		return uuid;
	}

	public void setUuid(String uuid) {
		this.uuid = uuid;
	}

	public String getTempName() {
		return tempName;
	}

	public void setTempName(String tempName) {
		this.tempName = tempName;
	}

	public String getCountry() {
		return country;
	}

	public void setCountry(String country) {
		this.country = country;
	}

	public String getProvince() {
		return province;
	}

	public void setProvince(String province) {
		this.province = province;
	}

	public String getCity() {
		return city;
	}

	public void setCity(String city) {
		this.city = city;
	}

	public String getCityArea() {
		return cityArea;
	}

	public void setCityArea(String cityArea) {
		this.cityArea = cityArea;
	}

	public String getDeliveryTime() {
		return deliveryTime;
	}

	public void setDeliveryTime(String deliveryTime) {
		this.deliveryTime = deliveryTime;
	}

	public String getIsFree() {
		return isFree;
	}

	public void setIsFree(String isFree) {
		this.isFree = isFree;
	}

	public String getPriceType() {
		return priceType;
	}

	public void setPriceType(String priceType) {
		this.priceType = priceType;
	}

	public String getDefaultLimit() {
		return defaultLimit;
	}

	public void setDefaultLimit(String defaultLimit) {
		this.defaultLimit = defaultLimit;
	}

	public String getDefaultCost() {
		return defaultCost;
	}

	public void setDefaultCost(String defaultCost) {
		this.defaultCost = defaultCost;
	}

	public String getStepLimit() {
		return stepLimit;
	}

	public void setStepLimit(String stepLimit) {
		this.stepLimit = stepLimit;
	}

	public String getStepCost() {
		return stepCost;
	}

	public void setStepCost(String stepCost) {
		this.stepCost = stepCost;
	}

	public String getSetFree() {
		return setFree;
	}

	public void setSetFree(String setFree) {
		this.setFree = setFree;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getNumStr() {
		return numStr;
	}

	public void setNumStr(String numStr) {
		this.numStr = numStr;
	}

	public String getPageFlag() {
		return pageFlag;
	}

	public void setPageFlag(String pageFlag) {
		this.pageFlag = pageFlag;
	}

	public String getLevel() {
		return level;
	}

	public void setLevel(String level) {
		this.level = level;
	}

	public String getAreaPcode() {
		return areaPcode;
	}

	public void setAreaPcode(String areaPcode) {
		this.areaPcode = areaPcode;
	}

}
